<template>
<div class="photo-detail-container">
  <div class="photo-detail-img">
    <mt-swipe :show-indicators="false" :auto="0" @change="imgChange">
      <mt-swipe-item v-for="(v,k) in albumMediaList" :key="k">
        <img class="photo-swipe vCenter" v-if="v.mediaType==1" :src="v.mediaUrl.substr(0,4).toLowerCase() == 'http' ? v.mediaUrl : resUrl+v.mediaUrl" @click="showImg(v.mediaUrl,albumMediaList)">
        <!--<video class="photo-swipe-video" controls="controls" v-if="v.mediaType==2" :src="v.mediaUrl.substr(0,4).toLowerCase() == 'http' ? v.mediaUrl : resUrl+v.mediaUrl"></video>-->
        <div class="video-box" v-if="v.mediaType==2">
          <video class="video" :src="v.mediaUrl.substr(0,4).toLowerCase() == 'http' ? v.mediaUrl : resUrl+v.mediaUrl" controls controlsList="noremoteplayback nodownload" webkit-playsinline playsinline x5-playsinline x-webkit-airplay="allow"></video>
        </div>

      </mt-swipe-item>
    </mt-swipe>
    <span class="phpto-num" v-if="albumMediaList.length != 0">{{imgIndex+1}}/{{albumMediaList.length}}</span>
    <div class="photo-detail-box" :class="{active:isOpen}" v-if="collectionDetail.name||collectionDetail.dynastyName||collectionDetail.presentation">
      <h3 class="photo-title">{{collectionDetail.name?collectionDetail.name:'暂无名称'}}</h3>
      <p class="photo-detail-dynasty">{{collectionDetail.dynastyName?collectionDetail.dynastyName:'暂无朝代'}}</p>
      <div class="photo-detail-text">
        <p v-html="collectionDetail.presentation"></p>
      </div>
      <span v-if="collectionDetail.presentation" class="photo-detail-open" @click="openDetail"></span>
    </div>
  </div>

  <ul class="photo-detail-option">
    <li class="photo-back" @click="goBack"></li>
    <li class="photo-edit" @click="showEditBox"></li>
    <li class="photo-share" @click="clickShare"></li>
    <li class="photo-delete" @click="removeMedia"></li>
  </ul>


  <!--弹出框-->
  <div class="dialog" v-show="isEditBox">
    <div class="weui-mask"></div>
    <div class="weui-dialog">
      <div class="weui-dialog__bd">
        <input class="weui-input" type="text" placeholder="请输入照片名称" v-model="name">
        <div class="dynasty-list">
          <p class="dynasty-select" :class="{selectAll:!dynastyId}" @click="dropDown">{{dynastyName}}</p>
          <ul class="dynasty-dropdown" v-show="isDropDown">
            <li @click="selectDynasty('all')">请选择朝代</li>
            <li v-for="(v,k) in dynastyList" :key="k" @click="selectDynasty(v)">{{v.name}}</li>
          </ul>
        </div>
        <textarea class="weui-textarea" placeholder="藏品其他说明信息，如购买地点、出土地点、预估价等" v-model="presentation"></textarea>
        <p class="edit-prompt">为照片/视频添加名称、朝代方便您搜索查询</p>
      </div>
      <div class="weui-dialog__ft">
        <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default" @click.prevent="cancelEdit">取消</a>
        <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary" @click.prevent="saveEdit">确定</a>
      </div>
    </div>
  </div>

</div>
</template>

<script>
  import {ipconfig} from '../../../../../assets/js/ipconfig';
  import {isWeiXin} from "../../../../../assets/js/tools";
  import {saveCollection,getCollectionDetail,deleteMedia,getALLDynastyList} from '../../../../../assets/js/getData';
  export default {
    name: "photoDetail",
    data(){
      return {
        resUrl:ipconfig.resUrl,//图片、视频、音频等资源访问路径
        isOpen:false,
        photoId:null,
        collectionDetail:{},//藏品详情
        albumMediaList:[],
        isEditBox:false,//是否显示编辑框
        dynastyList:[],//朝代列表
        dynastyName:'请选择朝代',
        dynastyId:null,//当前选择朝代ID
        isDropDown:false,//是否显示朝代下拉框
        name:'',//照片名称
        presentation:'',//照片介绍
        imgIndex:0,//媒体图片下标

      }
    },
    created(){
      this.photoId = this.$route.query.photoId;
      //获取藏品详情
      this.getCollectionDetail();
    },
    methods:{
      showImg(url,mediaList){
        let urlList = [];
        for(let i=0;i<mediaList.length;i++){
          urlList.push(mediaList[i].mediaUrl.substr(0,4).toLowerCase() == 'http' ? mediaList[i].mediaUrl : this.resUrl+mediaList[i].mediaUrl)
        }
        if(isWeiXin()){
          WeixinJSBridge.invoke('imagePreview', {
            'current' : url.substr(0,4).toLowerCase() == 'http' ? url : this.resUrl+url,
            'urls' : urlList
          });
        }
      },

      //删除媒体文件
      removeMedia(){
        let params = {
          id:this.albumMediaList[this.imgIndex].id,
          albumId:this.photoId,
          mediaType:this.albumMediaList[this.imgIndex].mediaType,
        };
        MessageBox.confirm('确定删除选中照片(视频)吗？删除后，无法恢复','').then(() => {
          deleteMedia(params).then(res => {
            if(res.data.reqResult.code > 0){
              this.getCollectionDetail();
            }else {
              Toast(res.data.reqResult.msg);
            }
          })
        }).catch(()=>{})
      },
      //获取媒体图片下标
      imgChange(index){
        this.imgIndex = index;
      },
      //获取藏品详情
      getCollectionDetail(){
        getCollectionDetail(this.photoId).then(res => {
          if(res.data.reqResult.code > 0){
            console.log(res);
            this.collectionDetail = res.data.data;
            this.albumMediaList = this.collectionDetail.albumMediaList;
            this.name = this.collectionDetail.name;
            this.dynastyId = this.collectionDetail.dynastyId;
            this.dynastyName = this.collectionDetail.dynastyName?this.collectionDetail.dynastyName:'请选择朝代';
            this.presentation = this.collectionDetail.presentation;
          }else {
            Toast(res.data.reqResult.msg);
          }
        })
      },
      //保存编辑
      saveEdit(){
        let params = {
          id:this.photoId,
          name:this.name,
          dynastyId:this.dynastyId,
          presentation:this.presentation,
        };
        saveCollection(params).then(res => {
          if(res.data.reqResult.code > 0){
            Toast('保存藏品成功');
            this.isEditBox = false;
            this.getCollectionDetail();
          }else {
            Toast(res.data.reqResult.msg);
          }
        })
      },
      //显示详情介绍
      openDetail(){
        this.isOpen = !this.isOpen;
        this.collectionDetail.presentation = this.collectionDetail.presentation.replace(/\n/g,'<br>');
      },
      //显示朝代下拉框
      dropDown(){
        this.isDropDown = !this.isDropDown;
      },
      //选择朝代
      selectDynasty(v){
        if(v=='all'){
          this.dynastyId = null;
          this.dynastyName = '请选择朝代';
        }else {
          this.dynastyId = v.id;
          this.dynastyName = v.name;
        }
        this.isDropDown = !this.isDropDown;
      },
      //显示编辑框
      showEditBox(){
        this.isEditBox = true;
        //获取所有朝代列表
        getALLDynastyList().then(res => {
          if(res.data.reqResult.code > 0){
            this.dynastyList = res.data.data;
          }else {
            Toast(res.data.reqResult.msg);
          }
        });
      },
      //取消编辑
      cancelEdit(){
        this.isEditBox = false;
      },
      //返回
      goBack(){
        // this.$router.go(-1);
        this.$router.replace({name:'collection.photo'});
      },
      //点击分享
      clickShare(){
        MessageBox('', '请点击右上角“...”将相册分享给好友鉴赏吧');
      },
    }
  }
</script>

<style scoped>
  .photo-detail-container{
    position: relative;
    height: 100vh;
    background-color: #000000;
    padding-top: 1.4rem;
    padding-bottom: 3.8rem;
  }
  .photo-detail-img{
    height: 100%;
    position: relative;
  }
  .photo-swipe{
    width: 100%;
  }
  .video-box{
    width: 100%;
    height: 100%;
    position: relative;
  }
  .video{
    width: 100%;
    height: 100%;
  }
  video::-webkit-media-controls-panel{
    background-color: transparent;
  }
  video::-webkit-media-controls-mute-button{
    display: none;
  }
  video::-webkit-media-controls-fullscreen-button{
    display: block !important;
  }
  video::-internal-media-controls-download-button{
    display: none;
  }
  video::-webkit-media-controls-volume-slider{
    display: none;
  }
  video::-webkit-media-controls-current-time-display{
    color: #ffffff;
    font-size: 13px;
  }
  video::-webkit-media-controls-time-remaining-display{
    display: block !important;
    color: #ffffff;
    font-size: 13px;
  }

  .phpto-num{
    display: inline-block;
    background-color: rgba(0,0,0,0.65);
    color: #ffffff;
    font-size: 0.5rem;
    padding: 0.05rem 0.4rem;
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    line-height: 0.5rem;
  }
  .photo-detail-box{
    width: 5.25rem;
    border-radius: 2px;
    background-color: rgba(0,0,0,0.62);
    color: #ffffff;
    padding: 0.5rem 0.5rem 0.45rem 0.4rem;
    position: absolute;
    top: 0.75rem;
    left: 0.95rem;
    box-sizing: border-box;
    border: 1px solid rgba(255, 255, 255, 0.8);
  }
  .photo-title{
    display: inline-block;
    width: 4.4rem;
    border-bottom: 1px solid #ffffff;
    font-size: 0.7rem;
    font-weight: normal;
    padding-left: 0.95rem;
    height: 1.3rem;
    line-height: 1.3rem;
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    background-image: url("../../../../../../static/images/collection/bi_title.png");
    background-repeat: no-repeat;
    background-size: 0.75rem 0.75rem;
    background-position: left center;
  }
  .photo-detail-dynasty{
    font-size: 0.6rem;
    padding-left: 0.95rem;
    height: 1.2rem;
    line-height: 1.2rem;
  }
  .photo-detail-open{
    display: inline-block;
    width: 1.2rem;
    height: 1.2rem;
    background-image: url("../../../../../../static/images/collection/open_1.png");
    background-repeat: no-repeat;
    background-size: 0.5rem 0.525rem;
    background-position: 0.3rem 0.3rem;
    position: absolute;
    right: 0;
    bottom: 0;
  }
  .photo-detail-text{
    display: none;
    width: 16.15rem;
    padding-left: 0.95rem;
    padding-right: 0.55rem;
    padding-bottom: 1.2rem;
    font-size: 0.6rem;
    line-height: 0.9rem;
    margin-top: 0.4rem;
    box-sizing: border-box;
  }
  .photo-detail-text p{
    word-break:break-all;
  }
  .photo-detail-box.active{
    width: 91%;
  }
  .photo-detail-box.active .photo-title{
    min-width: 4.4rem;
    width: auto;
  }
  .photo-detail-box.active .photo-detail-text{
    display: block;
  }
  .photo-detail-box.active .photo-detail-open{
    background-image: url("../../../../../../static/images/collection/open_2.png");
  }
  .photo-detail-option{
    width: 100%;
    height: 2.3rem;
    background-color: #ffffff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2.1rem;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
  }
  .photo-detail-option li{
    width: 1.15rem;
    height: 1.1rem;
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
  }
  .photo-back{
    background-image: url("../../../../../../static/images/collection/back.png");
  }
  .photo-edit{
    background-image: url("../../../../../../static/images/collection/edit.png");
  }
  .photo-share{
    background-image: url("../../../../../../static/images/collection/share.png");
  }
  .photo-delete{
    background-image: url("../../../../../../static/images/collection/delete.png");
  }
  /*表单placeholder样式*/
  input::-webkit-input-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input:-moz-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input::-moz-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  input:-ms-input-placeholder{
    color: #666666;
    font-size: 0.7rem;
  }
  textarea::-webkit-input-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea:-moz-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea::-moz-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  textarea:-ms-input-placeholder {
    color: #666666;
    font-size: 0.55rem;
  }
  .weui-dialog{
    background: #ffffff;
    border-radius: 2px;
    max-width: 14rem;
  }
  .weui-dialog__bd{
    padding: 1.8rem 1rem 1.3rem 1rem;
  }
  .weui-input,.dynasty-list,.weui-textarea{
    width: 100%;
    height: 1.5rem;
    line-height: 1.5rem;
    background-color: #EEEEEE;
    border-radius: 4px;
    padding: 0 0.7rem;
    box-sizing: border-box;
    color: #333333;
    font-size: 0.7rem;
    margin-bottom: 0.9rem;
  }
  .dynasty-list{
    position: relative;
  }
  .dynasty-list:after{
    content: ' ';
    display: inline-block;
    width: 0.45rem;
    height: 0.25rem;
    background-image: url("../../../../../../static/images/collection/sanjiao.png");
    background-repeat: no-repeat;
    background-size: 0.45rem;
    position: absolute;
    top: 0.7rem;
    right: 0.7rem;
    z-index: 10;
  }
  .dynasty-select{
    text-align: left;
    height: 100%;
  }
  .dynasty-select.selectAll{
    color: #666666;
  }
  .dynasty-dropdown{
    background-color: #ffffff;
    border-radius: 4px;
    box-shadow: 0 0 5px 0 rgba(0,0,0,0.3);
    padding: 0.2rem 0;
    max-height: 8rem;
    overflow: auto;
    position: absolute;
    top: 1.65rem;
    left: 0;
    right: 0;
    z-index: 10;
  }
  .dynasty-dropdown li{
    height: 1.3rem;
    line-height: 1.3rem;
    border-bottom: 1px solid #EEEEEE;
    color: #333333;
    font-size: 0.6rem;
    text-align: center;
  }
  .dynasty-dropdown li:last-child{
    border-bottom: none;
  }
  .weui-textarea{
    height: 4.25rem;
    line-height: 0.7rem;
    font-size: 0.55rem;
    padding-top: 0.45rem;
    padding-bottom: 0.45rem;
  }
  .edit-prompt{
    color: #282828;
    font-size: 0.7rem;
    line-height: 1rem;
    letter-spacing: 1px;
    text-align: left;
  }
  .weui-dialog__btn_default{
    color: #999999;
    font-size: 0.75rem;
  }
</style>
